const os = require('os');
const cluster = require('cluster');
const config = require('./config');
const amqp = require('amqplib');
// const messageBody = require("./messageBody");
// const msgProcWork = require("./msgProcWork");
// require('./XDate');





//var cpuCount = require('os').cpus().length;
var cpuCount = 1;

if (cluster.isMaster) {
    for (var i = 0; i < cpuCount; ++i) {
        cluster.fork();
    }
} else {

    amqp.connect(config.connectionString).then(function(conn) {
        process.once('SIGINT', function() { conn.close(); });
        return conn.createChannel().then(function(ch) {
            var ok = ch.assertQueue(config.queueName, { durable: true });
            ok = ok.then(function(_qok) {
                return ch.consume(config.queueName, function(msg) {
                    ch.ack(msg);
                    console.log(msg.content.toString());
                    // var message = messageBody.getStandard(msg.content.toString());
                    // if (message != null) {
                    //     console.log(" [x] Received:");
                    //     console.log(message);
                    //     if (msgProcWork[message.type]) {
                    //         msgProcWork[message.type].messageProc(message);
                            
                    //     } else {
                    //         console.log('暂时不支持:' + message.type + "类型的消息");
                    //     }
                    // } else {
                    //     console.log('非标准消息:' + msg.content.toString());
                    // }
                });
            });

            return ok.then(function(_consumeOk) {
                console.log('Waiting for messages. To exit press CTRL+C');
            });
        });
    }).catch(console.warn);
}